/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.action;

import java.io.Serializable;

import com.sencha.gxt.data.shared.loader.LoadEvent;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.data.shared.loader.PagingLoadResultBean;
import com.sencha.gxt.widget.core.client.toolbar.PagingToolBar;


/**
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 * @param <B>
 *           the type of the beans returned in the {@link PagingLoadResult}
 * @param <C>
 *           the type of the given {@link PagingLoadConfig}
 * @param <D>
 *           the type of the RPC returned {@link PagingLoadResult}
 */
public class DkPagingToolbar<B extends Serializable, C extends PagingLoadConfig, D extends PagingLoadResult<B>> extends
		PagingToolBar
{

	private boolean showMenu = false;

	/**
	 * Wrapper to {@link PagingToolBar#PagingToolBar(ToolBarAppearance, PagingToolBarAppearance, int)}
	 * 
	 * @param pageSize
	 */
	public DkPagingToolbar(final int pageSize)
	{
		super(pageSize);
	}

	/**
	 * Wrapper to {@link PagingToolBar#PagingToolBar(ToolBarAppearance, PagingToolBarAppearance, int)}
	 * 
	 * @param toolBarAppearance
	 * @param appearance
	 * @param pageSize
	 */
	public DkPagingToolbar(final ToolBarAppearance toolBarAppearance, final PagingToolBarAppearance appearance,
			final int pageSize)
	{
		super(toolBarAppearance, appearance, pageSize);
	}

	@SuppressWarnings("unchecked")
	private DkBasePagingLoader<B, C, D> getLoader()
	{
		return (DkBasePagingLoader<B, C, D>) loader;
	}

	/**
	 * Overridden method to add action behaviour
	 */
	@SuppressWarnings("unchecked")
	@Override
	protected void doLoadRequest(final int offset, final int limit)
	{
		if (isReuseConfig() && config != null)
		{
			config.setOffset(offset);
			config.setLimit(pageSize);
			getLoader().loadWithActionExecute((C) config);
		}
		else
		{
			loader.setLimit(pageSize);
			getLoader().loadWithActionExecute(limit, offset);
		}
	}

	/**
	 * Manually update total length and update toolbar display accordingly
	 * 
	 * @param totalLength
	 *           the new total length
	 */
	public void manuallyUpdateTotalLength(final int totalLength)
	{
		final PagingLoadResult<B> result = new PagingLoadResultBean<B>();
		result.setOffset(start);
		result.setTotalLength(totalLength);
		final LoadEvent<PagingLoadConfig, PagingLoadResult<?>> event = new LoadEvent<PagingLoadConfig, PagingLoadResult<?>>(config,
				result);
		onLoad(event);
	}

	/**
	 * @return the showMenu
	 */
	public boolean isShowMenu()
	{
		return showMenu;
	}

	/**
	 * @param showMenu
	 *           the showMenu to set
	 */
	public void setShowMenu(final boolean showMenu)
	{
		this.showMenu = showMenu;
	}

	@Override
	protected void initMore()
	{
		if (isShowMenu())
		{
			super.initMore();
		}
	}

	/**
	 * @return the total length
	 */
	public int getTotalLength()
	{
		return totalLength;
	}
}
